package me.shurufa.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import me.shurufa.R;
import me.shurufa.fragments.DigestFragment;
import me.shurufa.model.BookInfoResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.LogUtils;

/**
 * 书摘列表页
 *
 * @FileName: me.shurufa.activities.DigestActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-01 18:03
 */
public class DigestActivity extends BaseActivity{

    private long menuId;
    private String menuTitle;
    private long bookId;
    private long userId;
    private int fromWhere;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_digest;
        super.onCreate(savedInstanceState);
        menuId = getIntent().getLongExtra(Constants.ARG_MENU_ID,0L);
        menuTitle = getIntent().getStringExtra(Constants.ARG_MENU_TITLE);
        bookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID,0L);
        userId = getIntent().getLongExtra(Constants.ARG_USER_ID,0L);
//        把是不是从我的收藏进入的flag 传递给fragment
        fromWhere = getIntent().getIntExtra(Constants.ARG_FROM_WHERE,0);
        LogUtils.kLog().e("digestActivity-fromwhere:"+fromWhere);
        if (menuId==0L&&fromWhere!=Constants.TYPE_FROM_MY_FAVOURITE){
            return;
        }
        uiInit();
        initData();
    }

    private void initData() {
        DigestFragment digestFragment = DigestFragment.newInstance(menuId,menuTitle,userId,fromWhere,bookId);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container,digestFragment);
        transaction.commit();
    }

    private void uiInit() {
        new RequestServerTask<BookInfoResponse>(BookInfoResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<>();
                params.put("bookId", bookId);
                return HttpUtil.post(NetworkConstants.GET_BOOK_INFO, params);
            }

            @Override
            protected void onSuccess(BookInfoResponse result) {
                if (result.data!=null&&!TextUtils.isEmpty(result.data.title))
                    titleTextView.setText(result.data.title);
            }
        }.start();
    }
}
